/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace DslEditorPowerToy.Languages.EditorConfiguration
{
    internal static class ComponentModelHelper
    {
        #region Public Methods
        /// <summary>
        /// Removes descriptor for specified name from descriptor collection
        /// </summary>
        /// <param name="descriptors"></param>
        /// <param name="propertyName"></param>
        public static void RemoveDescriptor(PropertyDescriptorCollection descriptors, string propertyName)
        {
            if (true == string.IsNullOrEmpty(propertyName))
                return;

            //Find descriptor for property
            PropertyDescriptor descriptor = descriptors.Find(propertyName, true);
            if (null != descriptor)
                descriptors.Remove(descriptor);
        }
        #endregion
        /// <summary>
        /// Removes all but the specified descriptors from collection
        /// </summary>
        /// <param name="descriptors"></param>
        /// <param name="p"></param>
        public static void RemoveAllDescriptorsBut(PropertyDescriptorCollection descriptors, string[] propertyNames)
        {
            if ((null == propertyNames)
                || (0 == propertyNames.Length))
                return;

            List<string> propertyNameList = new List<string>(propertyNames);

            //Get list of descriptors to remove
            List<string> descriptorsToRemove = new List<string>();
            foreach (PropertyDescriptor descriptor in descriptors)
            {
                if (false == propertyNameList.Contains(descriptor.Name))
                    descriptorsToRemove.Add(descriptor.Name);
            }

            //Remove descriptors
            foreach (string descriptorName in descriptorsToRemove)
                RemoveDescriptor(descriptors, descriptorName);
        }
    }
}
